#!/bin/sh

http_server="172.24.1.122";
usage1="CentOS7,CentOS8 usage: ./dist_centos_exporter.sh el78"
usage2="CentOS6.x usage:  ./dist_centos_exporter.sh el6"

if [ $# -ne 1 ]; then
	echo $usage1
	echo $usage2
elif [[ $1 = 'el78' ]] || [[ $1 = 'el6' ]]; then
# 从infra的http服务器拉取exporter程序文件、配置文件到应用服务器本地
	wget http://${http_server}/pcm/node_exporter -O /usr/local/bin/node_exporter
	sleep 1
	echo "HAH Infra/> Prometheus node_exporter download complish."
	wget http://${http_server}/pcm/process-exporter -O /usr/local/bin/process-exporter
	sleep 1
	echo "HAH Infra/> Prometheus process-exporter download complish."
	mkdir -p /etc/process-exporter
	wget http://${http_server}/pcm/process-exporter-config.yml -O /etc/process-exporter/config.yml
	sleep 1
	echo "HAH Infra/> Prometheus process-exporter configuration file download complish."

	if [[ $1 = 'el78' ]]; then
	# 从infra的http服务器拉取exporter服务注册文件
		wget http://${http_server}/pcm/el7_node_exporter.service -O /etc/systemd/system/node_exporter.service
		echo "HAH Infra/> node_exporter service registry file download complish."
		sleep 1
		wget http://${http_server}/pcm/el7_process-exporter.service -O /etc/systemd/system/process-exporter.service
		echo "HAH Infra/> process-exporter service registry file download complish."
		sleep 1
	elif [[ $1 = 'el6' ]]; then
		#注意：CentOS6.x使用以下服务注册文件，并赋予可执行权限
		wget http://${http_server}/pcm/el6_node_exporter.service -O /etc/init.d/node_exporter
		echo "HAH Infra/> node_exporter service registry file download complish."
		sleep 1
		wget http://${http_server}/pcm/el6_process-exporter.service -O /etc/init.d/process-exporter
		echo "HAH Infra/> process-exporter service registry file download complish."
		sleep 1
		chmod 755 /etc/init.d/node_exporter
		chmod 755 /etc/init.d/process-exporter
		echo "HAH Infra/> CentOS6.x, service registry file add executable previlidge complish."
	fi

	#exporter程序增加可执行权限、创建prometheus专属系统用户
	chmod 755 /usr/local/bin/node_exporter
	echo "HAH Infra/> node_exporter add executable previlidge complish."
	chmod 755 /usr/local/bin/process-exporter
	echo "HAH Infra/> process-exporter add executable previlidge complish."

	groupadd prometheus
	useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus
	chown prometheus.prometheus -R /var/lib/prometheus/
	echo "HAH Infra/> create user [prometheus] complish."
	
	if [[ $1 = 'el78' ]]; then
		#将exporter注册为Linux系统服务，实现开机自启动
		systemctl daemon-reload
		systemctl enable node_exporter
		systemctl enable process-exporter
		echo "HAH Infra/> CentOS78, exporter routines have been registried as Linux system service."
		#启动exporter
		systemctl start node_exporter
		systemctl start process-exporter
		echo "HAH Infra/> CentOS78, exporter service started."
	elif [[ $1 = 'el6' ]]; then
		#注意：CentOS6.x使用
		chkconfig node_exporter on 
		chkconfig process-exporter on
		echo "HAH Infra/> CentOS6, exporter routines have been registried as Linux system service."
		service node_exporter start
		service process-exporter start
		echo "HAH Infra/> CentOS6, exporter service started."
	fi
else
	echo $usage1
	echo $usage2
fi